<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class OperationLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param  string|null $guard
     * @return mixed
     */
    public function handle(Request $req, Closure $next)
    {
        $user_id = 0;
        if(Auth::guard('admin')->check()) {
            $user_id = (int) Auth::guard('admin')->user()->id;
        }
        $input = $req->all();
        $data['user_id'] =  $user_id;
        $data['path'] = $req->path();
        $data['method'] = $req->method();
        $data['ip'] = $req->ip();
        $data['input'] = json_encode($input, JSON_UNESCAPED_UNICODE);
        //记录boss请求和返回数据
        $log = new Logger('operationlog');
        $log->pushHandler(new StreamHandler(storage_path('logs/operationlog.log'),Logger::INFO));
        $log->addInfo('访问操作记录：',$data);
        return $next($req);
    }
}
